package com.lrs.java.jdbc.jdbc_teacher;

//树状展现
//注意：Statement stmt = null;ResultSet rs = null;这两句话不能定义为成员变量

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

class ArticleMysql {

	public static void main(String[] args) throws Exception {
		new ArticleMysql().show();
	}

	public void show() throws Exception {
		Connection conn = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager
					.getConnection("jdbc:mysql://localhost/bbs?user=root&password=bjsxt");
			// conn = DriverManager
			// .getConnection("jdbc:mysql://localhost/bbs?user=root&password=root&useUnicode=true&characterEncoding=iso8859-1");
			tree(conn, 0, 0);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException ex) {
			System.out.println("SQLException: " + ex.getMessage());
			System.out.println("SQLState: " + ex.getSQLState());
			System.out.println("VendorError: " + ex.getErrorCode());
		} finally {
			try {
				if (conn != null) {
					conn.close();
					conn = null;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	private void tree(Connection conn, int id, int grade) throws Exception {
		Statement stmt = null;

		ResultSet rs = null;
		StringBuilder strPre = new StringBuilder();
		for (int i = 0; i < grade; i++) {
			strPre.append("    ");
		}
		try {
			stmt = conn.createStatement();
			String sql = "select * from article where pid = " + id;
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				String s = strPre + rs.getString("cont");
				// s = new String(s.getBytes("iso8859-1"),"gb2312");
				System.out.println(strPre + s);
				if (rs.getInt("isleaf") == 0) {
					tree(conn, rs.getInt("id"), grade + 1);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (stmt != null) {
					stmt.close();
					stmt = null;
				}
				/*
				 * if (conn != null) { conn.close(); conn = null; }
				 */
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}
}

// use bbs;

// set names gbk;

// DROP TABLE IF EXISTS article;

// CREATE TABLE `article` (
// `id` int(11) default NULL,
// `cont` varchar(200) default NULL,
// `pid` int(11) default NULL,
// `isleaf` int(11) default NULL
// ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

// insert into article values (1, '蚂蚁大战大象', 0, 0);
// insert into article values (2, '大象被打趴下了', 1, 0);
// insert into article values (3, '蚂蚁也不好过', 2, 1);
// insert into article values (4, '瞎说', 2, 0);
// insert into article values (5, '没有瞎说', 4, 1);
// insert into article values (6, '怎么可能', 1, 0);
// insert into article values (7, '怎么没有可能', 6, 1);
// insert into article values (8, '可能性是很大的', 6, 1);
// insert into article values (9, '大象进医院了', 2, 0);
// insert into article values (10, '护士是蚂蚁', 9, 1);